package com.project.easy.common.framework.web.servlet;

import  com.project.easy.common.util.constants.CommonConstant;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/10/1 23:16
 * @description:
 */
public final class HttpServletUtils {
    /**
     * 私有构造方法
     */
    private HttpServletUtils() {
    }
    /**
     * 通过工具类获取HttpServletRequest
     * @return servletRequest
     */
    public static HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        return  request;
    }
    /**
     * 通过工具类获取HttpServletRequest
     * @return HttpServletResponse
     */
    public static HttpServletResponse getHttpServletResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
        return  response;
    }
    /**
     * 获取Http 访问地址
     * @param request 请求地址
     * @return 返回URL
     */
    public static String getRequestUrl(HttpServletRequest request) {
        if (Objects.isNull(request)) {
            return CommonConstant.EMPTY_STRING;
        }
        return  request.getRequestURI();
    }
}
